<?php

namespace app\index\controller;

use think\Controller;
use think\Model;
use think\Request;
use app\index\controller\Base;

class Diary extends Base
{
    public function index()
    {
        return view('diary/index', ['list' => $this->list]);
    }

    /**
     * 日记主页展示
     * @return \think\response\Json
     *
     */
    public function read()
    {
        $model = Model('Diary');
        $res = $model->index();
        if ($res) {
            foreach ($res as $k => $v) {
                $res[$k]['time'] = date('Y-m-d H:i:s', $v['time']);
            }

            $data = [
                'status' => true,
                'msg' => $res,
            ];
            return json($data);
        } else {
            $data = [
                'status' => false,
                'msg' => '暂无数据',
            ];
            return json($data);
        }
    }

    /**
     * 查看的文章
     *
     * @param $id 查看文章的id
     * @return \think\response\Json
     */
    public function readDiary($id)
    {
        $model = Model('diary');
        $res = $model->readDiary($id);
        $res['arr1']['time'] = date('Y-m-d H:i:s', $res['arr1']['time']);
        foreach ($res['arr2'] as $k => $v) {
            $res['arr2'][$k]['time'] = date('Y-m-d H:i:s', $v['time']);
        }
        return json($res);
    }

    /**
     * 发表评论
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function comment(Request $request)
    {
        $info = $request->post();
        $info['time'] = time();

        $model = Model('diary');
        $res = $model->comment($info);

        if ($res) {
            $data = [
                'status' => true,
                'msg' => '评论成功',
            ];
            return json($data);
        } else {
            $data = [
                'status' => false,
                'msg' => '评论失败,请稍后再试',
            ];
            return json($data);
        }
    }


    public function diary()
    {
        $arr = Request::instance()->post();
        $arr['time'] = time();
        $arr['uid'] = 2;
        $model = Model('diary');
        $res = $model->commitDiary($arr);
        if($res) {
            $data = [
              'status' => true,
              'msg' => '添加成功,待审核',
            ];
            return json($data);
        }
    }
}